package com.ydc.ln.tutorial.gui.app.minieditor;

import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.Scrollbar;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JDialog;

/**
 * 实现文字颜色的编辑功能
 * 
 * @author ydc
 * @date 上午12:59:26 2013-4-13
 */
public class MenuColor extends JDialog
        implements
            ItemListener,
            ActionListener,
            TextListener,
            AdjustmentListener {

    /**
     * @Fields serialVersionUID :
     */
    private static final long serialVersionUID = 655377579309061653L;

    // AWT组件声明
    CheckboxGroup gp;
    Checkbox fore, back;
    Scrollbar scrollbarRed, scrollbarGreen, scrollbarBlue;
    TextField textFieldRed, textFieldGreen, textFieldBlue;
    Button colorButtonOK, colorButtonCancel;
    Checkbox colorCheckbox;
    TextField colorTextField;

    // 改变标记
    boolean changed = true;

    // 同步标记
    boolean synchronism = false;

    // 颜色数组
    Color[] fbgc = new Color[2];
    Color[] fbgcOld = new Color[2];

    // 前景色标记
    boolean isFore = true;

    // 构造方法
    MenuColor(Frame frame, boolean modal) {
        super(frame, modal);
    }

    // myLayout方法，窗体布局
    public Color[] myLayout(Color fgc, Color bgc) {
        fbgc[0] = fgc;
        fbgc[1] = bgc;
        fbgcOld[0] = fgc;
        fbgcOld[1] = bgc;

        this.getContentPane().setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.CENTER;

        // 初始化色调选择组件
        gp = new CheckboxGroup();
        fore = new Checkbox("Foreground", true, gp);
        back = new Checkbox("Background", false, gp);
        gbc.gridx = 1;
        gbc.gridy = 0;
        getContentPane().add(fore, gbc);
        gbc.gridx = 3;
        gbc.gridy = 0;
        getContentPane().add(back, gbc);
        fore.addItemListener(this);
        back.addItemListener(this);

        // 初始化Red标签
        Label labelRed = new Label("Red");
        gbc.gridx = 0;
        gbc.gridy = 1;
        getContentPane().add(labelRed, gbc);
        scrollbarRed = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 256);
        scrollbarRed.setValue(fgc.getRed());
        gbc.gridx = 1;
        gbc.gridy = 1;
        getContentPane().add(scrollbarRed, gbc);
        scrollbarRed.addAdjustmentListener(this);
        textFieldRed = new TextField(3);
        textFieldRed.setText(scrollbarRed.getValue() + "");
        textFieldRed.addTextListener(this);
        textFieldRed.setEditable(false);
        gbc.gridx = 2;
        gbc.gridy = 1;
        getContentPane().add(textFieldRed, gbc);

        // 初始化Green标签
        Label labelGreen = new Label("Green");
        gbc.gridx = 0;
        gbc.gridy = 2;
        getContentPane().add(labelGreen, gbc);
        scrollbarGreen = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 256);
        scrollbarGreen.setValue(fgc.getGreen());
        gbc.gridx = 1;
        gbc.gridy = 2;
        getContentPane().add(scrollbarGreen, gbc);
        scrollbarGreen.addAdjustmentListener(this);
        textFieldGreen = new TextField(3);
        textFieldGreen.setText(scrollbarGreen.getValue() + "");
        textFieldGreen.addTextListener(this);
        textFieldGreen.setEditable(false);
        gbc.gridx = 2;
        gbc.gridy = 2;
        getContentPane().add(textFieldGreen, gbc);

        // 初始化Blue标签
        Label labelBlue = new Label("Blue");
        gbc.gridx = 0;
        gbc.gridy = 3;
        getContentPane().add(labelBlue, gbc);
        scrollbarBlue = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 256);
        scrollbarBlue.setValue(fgc.getBlue());
        gbc.gridx = 1;
        gbc.gridy = 3;
        getContentPane().add(scrollbarBlue, gbc);
        scrollbarBlue.addAdjustmentListener(this);
        textFieldBlue = new TextField(3);
        textFieldBlue.setText(scrollbarBlue.getValue() + "");
        textFieldBlue.addTextListener(this);
        textFieldBlue.setEditable(false);
        gbc.gridx = 2;
        gbc.gridy = 3;
        getContentPane().add(textFieldBlue, gbc);

        // 初始化OK按钮
        colorButtonOK = new Button("OK");
        gbc.gridx = 4;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.NONE;
        getContentPane().add(colorButtonOK, gbc);
        colorButtonOK.addActionListener(this);

        // 初始化Cancel按钮
        colorButtonCancel = new Button("Cancel");
        gbc.gridx = 4;
        gbc.gridy = 2;
        getContentPane().add(colorButtonCancel, gbc);
        colorButtonCancel.addActionListener(this);

        // 初始化Lock RGB按钮
        colorCheckbox = new Checkbox("Lock RGB", false);
        gbc.gridx = 3;
        gbc.gridy = 3;
        getContentPane().add(colorCheckbox, gbc);
        colorCheckbox.addItemListener(this);

        // 初始化颜色预览文本框
        colorTextField = new TextField("Java awt");
        colorTextField.setSize(90, 60);
        colorTextField.setForeground(fgc);
        colorTextField.setBackground(bgc);
        colorTextField
                .setFont(new Font("Courier", Font.BOLD + Font.ITALIC, 36));
        gbc.gridx = 3;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        getContentPane().add(colorTextField, gbc);

        // 添加窗体监听器
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                dispose();
            }
        });

        this.setLocation(120, 120);
        this.setResizable(false);
        this.setSize(480, 160);

        // 显示窗体
        this.setVisible(true);

        if (changed) {
            return fbgc;
        } else {
            return fbgcOld;
        }
    }


    // 数值调整监听方法
    @Override
    public void adjustmentValueChanged(AdjustmentEvent e) {
        if (e.getSource() == scrollbarRed) {

            // 红色数值调整
            if (synchronism) {
                textFieldRed.setText(scrollbarRed.getValue() + "");
                scrollbarGreen.setValue(scrollbarRed.getValue());
                textFieldGreen.setText(scrollbarRed.getValue() + "");
                scrollbarBlue.setValue(scrollbarRed.getValue());
                textFieldBlue.setText(scrollbarRed.getValue() + "");
                if (isFore) {
                    colorTextField.setForeground(setColorVarSrollbar());
                    fbgc[0] = setColorVarSrollbar();
                } else {
                    colorTextField.setBackground(setColorVarSrollbar());
                    fbgc[1] = setColorVarSrollbar();
                }
            } else {
                textFieldRed.setText(scrollbarRed.getValue() + "");
                if (isFore) {
                    colorTextField.setForeground(setColorVarSrollbar());
                    fbgc[0] = setColorVarSrollbar();
                } else {
                    colorTextField.setBackground(setColorVarSrollbar());
                    fbgc[1] = setColorVarSrollbar();
                }
            }
        }
        // 绿色数值调整
        else if (e.getSource() == scrollbarGreen) {
            if (synchronism) {
                textFieldGreen.setText(scrollbarGreen.getValue() + "");
                scrollbarBlue.setValue(scrollbarGreen.getValue());
                textFieldBlue.setText(scrollbarGreen.getValue() + "");
                scrollbarRed.setValue(scrollbarGreen.getValue());
                textFieldRed.setText(scrollbarGreen.getValue() + "");
                if (isFore) {
                    colorTextField.setForeground(setColorVarSrollbar());
                    fbgc[0] = setColorVarSrollbar();
                } else {
                    colorTextField.setBackground(setColorVarSrollbar());
                    fbgc[1] = setColorVarSrollbar();
                }
            } else {
                textFieldGreen.setText(scrollbarGreen.getValue() + "");
                if (isFore) {
                    colorTextField.setForeground(setColorVarSrollbar());
                    fbgc[0] = setColorVarSrollbar();
                } else {
                    colorTextField.setBackground(setColorVarSrollbar());
                    fbgc[1] = setColorVarSrollbar();
                }
            }
        }
        // 蓝色数值调整
        else if (e.getSource() == scrollbarBlue) {
            if (synchronism) {
                textFieldBlue.setText(scrollbarBlue.getValue() + "");
                scrollbarRed.setValue(scrollbarBlue.getValue());
                textFieldRed.setText(scrollbarBlue.getValue() + "");
                scrollbarGreen.setValue(scrollbarBlue.getValue());
                textFieldGreen.setText(scrollbarBlue.getValue() + "");
                if (isFore) {
                    colorTextField.setForeground(setColorVarSrollbar());
                    fbgc[0] = setColorVarSrollbar();
                } else {
                    colorTextField.setBackground(setColorVarSrollbar());
                    fbgc[1] = setColorVarSrollbar();
                }
            } else {
                textFieldBlue.setText(scrollbarBlue.getValue() + "");
                if (isFore) {
                    colorTextField.setForeground(setColorVarSrollbar());
                    fbgc[0] = setColorVarSrollbar();
                } else {
                    colorTextField.setBackground(setColorVarSrollbar());
                    fbgc[1] = setColorVarSrollbar();
                }
            }
        }
    }

    // 文字改变监听方法
    @Override
    public void textValueChanged(TextEvent e) {
        if (e.getSource() == textFieldRed) {
            int i = 0;
            try {
                i = Integer.parseInt(textFieldRed.getText());
            } catch (NumberFormatException nfe) {

            }
            i = i < 0 ? 0 : i;
            i = i > 255 ? 255 : i;
            scrollbarRed.setValue(i);
            if (synchronism) {
                scrollbarGreen.setValue(scrollbarRed.getValue());
                textFieldGreen.setText(scrollbarRed.getValue() + "");
                scrollbarBlue.setValue(scrollbarRed.getValue());
                textFieldBlue.setText(scrollbarRed.getValue() + "");
                if (isFore) {
                    colorTextField.setForeground(setColorVarSrollbar());
                    fbgc[0] = setColorVarSrollbar();
                } else {
                    colorTextField.setBackground(setColorVarSrollbar());
                    fbgc[1] = setColorVarSrollbar();
                }
            } else {
                if (isFore) {
                    colorTextField.setForeground(setColorVarSrollbar());
                    fbgc[0] = setColorVarSrollbar();
                } else {
                    colorTextField.setBackground(setColorVarSrollbar());
                    fbgc[1] = setColorVarSrollbar();
                }
            }
        }
    }

    // 事件监听方法
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == colorButtonOK) {
            changed = true;
            dispose();
        } else if (e.getSource() == colorButtonCancel) {
            changed = false;
            dispose();
        }
    }

    // Item事件监听方法
    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getSource() == fore) {

            // 前景色改变
            isFore = true;
            scrollbarRed.setValue(fbgc[0].getRed());
            textFieldRed.setText(scrollbarRed.getValue() + "");
            scrollbarGreen.setValue(fbgc[0].getGreen());
            textFieldGreen.setText(scrollbarGreen.getValue() + "");
            scrollbarBlue.setValue(fbgc[0].getBlue());
            textFieldBlue.setText(scrollbarBlue.getValue() + "");
            colorTextField.setForeground(setColorVarSrollbar());
        } else if (e.getSource() == back) {

            // 背景色改变
            isFore = false;
            scrollbarRed.setValue(fbgc[1].getRed());
            textFieldRed.setText(scrollbarRed.getValue() + "");
            scrollbarGreen.setValue(fbgc[1].getGreen());
            textFieldGreen.setText(scrollbarGreen.getValue() + "");
            scrollbarBlue.setValue(fbgc[1].getBlue());
            textFieldBlue.setText(scrollbarBlue.getValue() + "");
            colorTextField.setBackground(setColorVarSrollbar());
        } else if (e.getSource() == colorCheckbox) {
            if (colorCheckbox.getState()) {
                synchronism = true;
            } else {
                synchronism = false;
            }
        }
    }

    private Color setColorVarSrollbar() {
        return new Color(scrollbarRed.getValue(), scrollbarGreen.getValue(),
                scrollbarBlue.getValue());
    }

}
